ggplot graphicssp packageggplot2The grammar of graphics: consistent aesthetics, multidimensional conditioning, and step-by-step plot building.
geom_: The geometric shapes representing dataaes(): Aesthetics of the geometric and statistical objects (color, size, shape, and position)scale_: Maps between the data and the aesthetic dimensionsdata
+ geometry,
+ aesthetic mappings like position, color and size
+ scaling of ranges of the data to ranges of the aesthetics
stat_: Statistical summaries of the data that can be plotted, such as quantiles, fitted curves (loess, linear models), etc.coord_: Transformation for mapping data coordinates into the plane of the data rectanglefacet_: Arrangement of data into grid of plotstheme: Visual defaults (background, grids, axes, typeface, colors, etc.)library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg))
p + geom_point()
p +
geom_point(aes(colour = factor(cyl)))
p +
geom_point(aes(shape = factor(cyl)))
qsecp +
geom_point(aes(size = qsec))
qsecp +
geom_point(aes(colour = factor(cyl),size = qsec))
p +
geom_point(aes(colour = factor(cyl),size = qsec,shape=factor(gear)))
p + geom_point() +
geom_smooth(method="lm")
p + geom_point(aes(colour = cyl)) +
scale_colour_gradient(low = "blue")
p + geom_point(aes(shape = factor(cyl))) +
scale_shape(solid = FALSE)
ggplot(mtcars, aes(wt, mpg)) +
geom_point(colour = "red", size = 3)
d <- ggplot(diamonds, aes(carat, price))
d + geom_point(alpha = 0.2)
Varying alpha useful for large data sets
d +
geom_point(alpha = 0.1)
d +
geom_point(alpha = 0.01)
Edit plot p above to include:
p+
geom_point()+
geom_smooth()+
geom_rug()
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_point()
p +
geom_jitter()
p +
geom_violin()
p +
geom_violin() + geom_jitter(position = position_jitter(width = .1))
Will come back to this next week for raster package.
Visualize a data transformation
..name.. syntaxstat_bin(geom="bar") OR geom_bar(stat="bin")Old Faithful Geyser Data on duration and waiting times.
library("MASS")
data(geyser)
m <- ggplot(geyser, aes(x = duration, y = waiting))
See ?geyser for details.
m +
geom_point()
m +
geom_point() + stat_density2d(geom="contour")
Check ?geom_density2d() for details
m +
geom_point() + stat_density2d(geom="contour") +
xlim(0.5, 6) + ylim(40, 110)
Update limits to show full contours. Check ?geom_density2d() for details
m + stat_density2d(aes(fill = ..level..), geom="polygon") +
geom_point(col="red")
Check ?geom_density2d() for details
Edit plot m to include:
binhex plot of the Old Faithful dataExperiment with the number of bins to find one that works.
See ?stat_binhex for details
m + stat_binhex(bins=10) +
geom_point(col="red")
b=ggplot(mpg,aes(fl))+
geom_bar( aes(fill = fl)); b
b + scale_fill_grey( start = 0.2, end = 0.8,
na.value = "red")
a <- ggplot(mpg, aes(hwy)) +
geom_dotplot( aes(fill = ..x..)); a
gradienta + scale_fill_gradient( low = "red",
high = "yellow")
gradient2a + scale_fill_gradient2(low = "red", high = "blue",
mid = "white", midpoint = 25)
gradientna + scale_fill_gradientn(
colours = rainbow(10))
b +
scale_fill_brewer( palette = "Blues")
Edit the contour plot of the geyser data to use a sequential brewer palette:
m +
stat_density2d(aes(fill = ..level..), geom="polygon") +
geom_point(col="red")
Note: usescale_fill_distiller() rather than scale_fill_brewer() for continuous data
m + stat_density2d(aes(fill = ..level..), geom="polygon") +
geom_point(size=.75)+
scale_fill_distiller(palette="OrRd",#breaks=c(0.005,0.008,0.01),
name="Kernel\nDensity")+
xlim(0.5, 6) + ylim(40, 110)+
xlab("Eruption Duration (minutes)")+
ylab("Waiting time (minutes)")
Or use geom=tile for a raster representation.
m + stat_density2d(aes(fill = ..density..), geom="tile",contour=F) +
geom_point(size=.75)+
scale_fill_distiller(palette="OrRd",
name="Kernel\nDensity")+
xlim(0.5, 6) + ylim(40, 110)+
xlab("Eruption Duration (minutes)")+
ylab("Waiting time (minutes)")
Create noisy exponential data
set.seed(201)
n <- 100
dat <- data.frame(
xval = (1:n+rnorm(n,sd=5))/20,
yval = 10^((1:n+rnorm(n,sd=5))/20)
)
Make scatter plot with regular (linear) axis scaling
sp <- ggplot(dat, aes(xval, yval)) + geom_point()
sp
Example from R Cookbook
log10 scaling of the y axis (with visually-equal spacing)
sp + scale_y_log10()
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")
Quickly change plot appearance with themes.
ggthemes package.library(ggthemes)
Or build your own!
p=ggplot(mpg, aes(x = cty, y = hwy, color = factor(cyl))) +
geom_jitter() +
labs(
x = "City mileage/gallon",
y = "Highway mileage/gallon",
color = "Cylinders"
)
p
p + theme_solarized()
p + theme_solarized(light=FALSE)
p + theme_excel()
p + theme_economist()
facet_wrap(): one variable
ggplot(mpg, aes(x = cty, y = hwy, color = factor(cyl))) +
geom_jitter()+
facet_wrap(~year)
facet_grid(): two variables
ggplot(mpg, aes(x = cty, y = hwy, color = factor(cyl))) +
geom_jitter()+
facet_grid(year~cyl)
Very useful for timeseries of spatial data.
ggsave()Save a ggplot with sensible defaults:
ggsave(filename, plot = last_plot(), scale = 1, width, height)
Save any plot with maximum flexibility:
pdf(filename, width, height) # open device
ggplot() # draw the plot(s)
dev.off() # close the device
Formats
and more…
p plot from above using png() and dev.off()light=FALSEbase_size in the theme + theme_solarized(base_size=24)png("assets/test1.png",width=600,height=300)
p + theme_solarized(light=FALSE)
dev.off()
## quartz_off_screen
## 2
png("assets/test2.png",width=600,height=300)
p + theme_solarized(light=FALSE, base_size=24)
dev.off()
## quartz_off_screen
## 2
Perhaps R’s best documented package: docs.ggplot2.org
Sources:
Licensing: